<?php
$db = new Database;
if(isset($_POST) and $_SERVER['REQUEST_METHOD']=="POST"){
    $firstname = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname = $_POST['lastname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $rpassword = $_POST['rpassword'];
    $email = $_POST['email'];
    $companyCode = $_POST['companyCode'];
    $captcha = $_POST['captcha-code'];
    $regCap = $_SESSION['captcha_number'];
    
    // For Uploading Image
    $file = $_FILES['file']['name']; // File name
    $size = $_FILES['file']['size']; // File Size
    $path = "images/avatar/";
    $valid_formats = array("jpg", "png","JPG");
    list($txt, $ext) = explode(".", $file);
    
    $error = 0;
    if($firstname=="" or $lastname=="" or $username=="" or $password=="" or $rpassword=="" or $companyCode==""){ //if empty or $captcha==""
        echo "Please fill those required fields";
    }else{
        if(!strlen($file)){
            echo "Please select a file.";
        }else{
            if(!in_array($ext,$valid_formats)){
                echo "Invalid File Format";
            }else{
                if (($size >= 2097152*2097152)){
                    echo "File too large. File must be less than 4 megabytes.";
                }else{
                    if(checkstrname($firstname)=="0" or checkstrname($lastname)=="0" or checkstrname($middlename)=="0"){
                        echo "Wrong format of name.";
                    }else{
                        if($password!=$rpassword){
                            echo "Password did not match";
                        }else{
                            if(strlen($password)<5 || strlen($password)>15){
                                echo "Please input password length from 5 to 15";
                            }else{
                                $numrowsUser = $db->query("SELECT * FROM tbuser where username = '$username' and is_active=1","numrows");
                                if($numrowsUser>0){
                                    echo "Your username has already taken, Please choose another";
                                }else{
                                    //if($captcha!=$regCap){
                                        //echo "Captcha code doesnt match.";
                                    //}else{
                                        $numrowsComp = $db->query("SELECT * FROM tbcompany where company_code = '$companyCode' and is_active=1","numrows");
                                        if($numrowsComp==1){
                                            $companyID = $db->query("SELECT id FROM tbcompany where company_code = '$companyCode' and is_active=1","row");
                                            $insert = array(
                                                "firstname"=>$firstname,
                                                "lastname"=>$lastname,
                                                "middlename"=>$middlename,
                                                "email"=>$email,
                                                "username"=>$username,
                                                "password"=>md5($password),
                                                "userlevel_id"=>2,
                                                "company_id"=>$companyID['id'],
                                                "is_active"=>1,
                                                "status"=>0,
                                                "extension"=>$ext,"steps"=>1);
                                            echo "true";
                                        $userID = $db->insert("tbuser",$insert);
                                        // Create Folder of the user for their image
                                            mkdir($path."/".$userID); // location
                                                mkdir($path."/".$userID."/1"); // location
                                                mkdir($path."/".$userID."/2"); // location
                                            
                                            // Resize an image
                                               if($ext=="jpg" || $ext=="jpeg" || $ext=="JPG"){
                                                    $uploadedfile = $_FILES['file']['tmp_name'];
                                                    $src = imagecreatefromjpeg($uploadedfile);
                                               }
                                               else if($ext=="png" || $ext=="PNG"){
                                                    $uploadedfile = $_FILES['file']['tmp_name'];
                                                    $src = imagecreatefrompng($uploadedfile);
                                               }
                                               else{
                                                    $src = imagecreatefromgif($uploadedfile);
                                               }
                                               echo $scr;
                                               
                                               list($width,$height)=getimagesize($uploadedfile);
                                               // Set 1
                                               $newwidth=100;
                                               $newheight=($height/$width)*$newwidth;
                                               $tmp=imagecreatetruecolor($newwidth,$newheight);
                                               
                                               imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                                               
                                               $thumbnail = $path."/".$userID."/"."1/yourAvatar_".$userID.".".$ext;
                                               
                                               imagejpeg($tmp,$thumbnail,100);
                                               
                                               // Set 2
                                               $newwidth1=60;
                                               $newheight1=($height/$width)*$newwidth1;
                                               $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
                                               
                                               imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
                                               
                                               $thumbnail1 = $path."/".$userID."/"."2/yourAvatar2_".$userID.".".$ext;
                                               
                                               imagejpeg($tmp1,$thumbnail1,100);
                                               
                                               imagedestroy($src);
                                               imagedestroy($tmp);
                                               imagedestroy($tmp1);
                                               
                                        }else{
                                            echo "Please enter valid Company Code";
                                        }
                                    //}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
?>